package com.czl.websocket.bean;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.czl.websocket.server.UserService;
import com.czl.websocket.type.MessageType;
import org.springframework.stereotype.Component;

/**
 * @author 石雷
 * shilei1@jiayuan.com
 * 2021/1/20/020 14:04
 */
@Component
public class Message {
    private  User user;
    private  String fromUid;
    private  String text;
    private  String toUid;
    private  String toGroupId;
    private int type;
    private int fileType;
    private String fileUrl;
    private  boolean self;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getFromUid() {
        return fromUid;
    }

    public void setFromUid(String fromUid) {
        this.fromUid = fromUid;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getToUid() {
        return toUid;
    }

    public void setToUid(String toUid) {
        this.toUid = toUid;
    }

    public String getToGroupId() {
        return toGroupId;
    }

    public void setToGroupId(String toGroupId) {
        this.toGroupId = toGroupId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getFileType() {
        return fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }

    public boolean isSelf() {
        return self;
    }

    public void setSelf(boolean self) {
        this.self = self;
    }

    public  static   Message getInstance(String json) {
        Message message = JSONUtil.toBean(json, Message.class);
          if(message!=null&& message.getFromUid()!=null&&!"".equals(message.getFromUid())){
            String fromUid = message.getFromUid();
            User userInfo = SpringUtil.getBean(UserService.class).getUserInfo(fromUid);
            message.setUser(userInfo);
        }
        return message;
    }

}
